
package org.apache.osgimaker.analyse.algorithm.dependency;

import org.apache.osgimaker.analyse.algorithm.graph.AtomicVertex;

/**
 * Processor of {@link Statement Statements} as defined in a 
 * dependency definition file.
 * 
 */
public class DependencyProcessor
{
  private final Statement[] _statements;
  private int _index;
  
  /**
   * Creates a new instance for the specified dependency definition. It also
   * parses the definition and creates all {@link Statement Statements}.
   * 
   * @param dependencyDefinition Dependency definition as read from a
   *        .ddf file.
   * @param properties Contains predefined properties and will also
   *        be populated by definition in <code>dependencyDefinition</code>.
   * @param renderer Renderer for processing results.
   * @throws IllegalArgumentException if <tt>dependencyDefinition</tt> 
   *        is invalid.
   */
  public DependencyProcessor(String dependencyDefinition, 
                             DependencyProperties properties, 
                             ResultRenderer renderer) 
  {
    _statements = new DependencyDefinitionParser(dependencyDefinition, 
                                                 properties, 
                                                 renderer).getStatements();
  }
  
  /**
   * Returns <tt>true</tt> if there are still unprocessed statements.
   * @return
   */
  public boolean hasMoreStatements() {
    return _index < _statements.length;
  }
  
  /**
   * Executes the next unprocessed statement and returns its result.
   * @param graph The graph to be checked by the statement.
   * @return <tt>null</tt> if there is no unprocessed statement.
   */
  public Result executeNextStatement(AtomicVertex[] graph) {
    return hasMoreStatements() ? _statements[_index++].execute(graph) : null;
  }
}
